(function($) {
	$.fn.cascade = function(opts) {
		var settings = $.extend({
			url : "",
			optionsText : '',
			optionsValue : '',
			emptyOption : null,
			value : '',
			callback : function() {
			}
		}, opts || {});

		$(this).change(function() {
			var url = settings.url + $(this).val();
			$.getJSON(url, function(jsons) {
				var selectModel = $(settings.value);
				selectModel.empty();
				if (settings.emptyOption != null) {
					selectModel.append('<option value="">' + settings.emptyOption + '</option>');
				}

				if (jsons.length == 0) {
					selectModel.empty();
				} else {
					for ( var i in jsons) {
						var json = jsons[i];
						selectModel.append("<option value = " + json[settings.optionsValue] + ">" + json[settings.optionsText] + "</option>");
					}
				}

				settings.callback(jsons);
			});
		});
	};

	$.UploaderImages = function(opts) {
		var settings = $.extend({
			server : "",
			pick : "",
			thumb : "",
			formData : {}
		}, opts || {});

		// 初始化Web Uploader
		var uploader = WebUploader.create({
			// 选完文件后，是否自动上传。
			auto : true,
			// swf文件路径
			swf : '/assets/webuploader/Uploader.swf',
			// 文件接收服务端。
			server : settings.server,
			// 选择文件的按钮。可选。
			// 内部根据当前运行是创建，可能是input元素，也可能是flash.
			pick : settings.pick,
			// 只允许选择图片文件。
			accept : {
				title : 'Images',
				extensions : 'gif,jpg,jpeg,bmp,png',
				mimeTypes : 'image/jpg,image/jpeg,image/png'
			},
			duplicate : true,
			formData : settings.formData
		});

		// 当有文件添加进来的时候
		uploader.on('fileQueued', function(file) {
			$img = $(settings.thumb);

			uploader.makeThumb(file, function(error, src) {
				if (error) {
					$img.replaceWith('<span>不能预览</span>');
					return;
				}

				$img.attr('src', src);
			}, 150, 0);
		});
	}

	$.myAjax = function(opts) {
		var settings = $.extend({
			type : "POST",
			dataType : "json",
			url : "",
			data : {},
		}, opts || {});
		$.ajax(settings);
	}

	$.fn.serializeObject = function() {
		var o = {};
		$.each(this.serializeArray(), function(index) {
			if (this['value'] != undefined && this['value'].length > 0) {
				if (o[this['name']]) {
					o[this['name']] = o[this['name']] + "," + this['value'];
				} else {
					o[this['name']] = this['value'];
				}
			}
		});
		return o;
	};

	$.fn.cleanForm = function() {
		$(':input', this).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
	};

	$.fn.confirmOperator = function(opts) {
		var setting = $.extend({
			msg : "该操作不可逆，确定进行吗？",
			evenName : "click"
		}, opts || {});
		$(this).on(setting.evenName, function(event) {
			var _this = this;
			var r = confirm(setting.msg);
			if (!r) {
				event.preventDefault();
			}
		});
	};

})(jQuery)